package cn.cinz.controller;


import cn.cinz.pojo.User;
import cn.cinz.service.UserService;
import cn.cinz.util.IdUtils;
import cn.cinz.util.SendSmsUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.*;


/**
 * 用户视图跳转
 */
@Controller
@CrossOrigin
@RequestMapping("/user")
public class UserController {

    //controller 调service层
    @Autowired //它可以对类成员变量、方法及构造函数进行标注，完成自动装配的工作  找到BookService接口 在xml中找到BookService的实现类，完成注入
    @Qualifier("UserServiceImpl") //接口有多个实现类，@Qualifier指明@Autowired具体注入哪个实现类
    private UserService userService;


    private HttpSession session = null;//初始化session


    //用户注册
    @RequestMapping("/register")
    public String register(@Param("userName") String userName, @Param("userPwd") String userPwd, @Param("userPhone") String userPhone, @Param("userSex") String userSex) throws IOException {
        //生成随机id
        String uuid = IdUtils.getId();
        User user = new User();
        user.setUserId(uuid);
        user.setUserName(userName);
        user.setUserPwd(userPwd);
        user.setUserPhone(userPhone);
        user.setUserSex(userSex);
        user.setUserRegisterDate(new Date(System.currentTimeMillis()));

        userService.addUser(user);

        return "redirect:/login.html";
    }


    //用户登陆
    @RequestMapping("/login")
    public String login(String userName, String userPwd, String userPhone, HttpServletRequest request) {
        //判断是否存在这个用户   如果存在则跳入首页，不存在则返回注册
        if (userName != null && !"".equals(userName.trim())) {
            User user = userService.getLogin(userName, userPwd, userPhone);
            if (user != null) {
                session = request.getSession();
                session.setAttribute("userName", user.getUserName());//session存放用户名
                session.setAttribute("user",user);//session存放全部用户信息
                return "redirect:/user/home.html";
            } else {
                return "redirect:/register.html";//重定向
            }
        } else {
            //判断是否输入了手机号  使用手机号一键登陆
            if (userPhone != null && !"".equals(userPhone.trim())) {
                User user = userService.getLogin(userName, userPwd, userPhone);
                if (user != null) {
                    session = request.getSession();
                    session.setAttribute("userName", user.getUserName());//session存放用户
                    return "redirect:/user/home.html";
                } else {
                    return "redirect:/register.html";//重定向
                }
            }

        }
        return "error";
    }


    //session回收  用户退出登录
    @RequestMapping("/removeSession")
    public String removeSession(HttpSession session) {
        session.removeAttribute("userName");

        return "redirect:/login.html";
    }


}
